Geographic data and maps

Amelia McNamara

August 18, 2016

Types of geographic data

There are three main types of geographic data:

People often also talk about images as geographic data, because so many maps come that way (tiles from googlemaps and OpenStreetMap, satellite imagery). So, we could also think about the difference between

Tiles

Mapbox has a nice explanation of how map tiles work. Let’s check it out!

Points

Making a map with points in R with leaflet

We’re going to be doing a lot of the stuff from the RStudio leaflet tutorial.

library(leaflet)
m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=-93.2650, lat=44.9778, popup="Minneapolis!")
m

We need more data!!

Lets look at storm data from the NOAA. It comes in a few files that we need to join together in order to use.

library(readr)
library(dplyr)
stormlocs <- read_csv("StormEvents_locations-ftp_v1.0_d2016_c20160810.csv")
stormdetails <- read_csv("StormEvents_details-ftp_v1.0_d2016_c20160810.csv")
stormlocs <- stormlocs %>%
  left_join(stormdetails, by="EVENT_ID")
lightning <- stormlocs %>%
  filter(EVENT_TYPE=="Lightning")

Mapping the lightning

Now, we can programmatically map them.

m <- leaflet(data=lightning) %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(~LONGITUDE, ~LATITUDE)
m

Find another storm type to map

Bonus– add popups!

Lines

I don’t have much to say here

Polygons

Lets start with something easy(ish) – circles

tornados <- stormlocs %>%
  filter(EVENT_TYPE=="Tornado")
m <- leaflet(data=tornados) %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addCircles(~LONGITUDE, ~LATITUDE, weight = 1, radius = ~DAMAGE_PROPERTY, popup = ~EVENT_NARRATIVE)
m

But usually, you want polygons

Polgyons come in shapefiles

Most boundaries (state, national, etc) are provided in terms of polygons. Major mapping software ArcGIS, from ESRI, has essentially set the standard formats.

State shapefiles

I got these from the Census. You can choose the resolution.

library(rgdal)
states <- readOGR("cb_2015_us_state_500k", layer = "cb_2015_us_state_500k", verbose = FALSE)

Need to count up some data

tornadocount <- tornados %>%
  group_by(STATE) %>%
  summarize(n=n())

Colors are tricky!

colorbrewer

colors <- c("#edf8fb", "#b2e2e2", "#66c2a4", "#238b45")
tornadocount <- tornadocount %>%
  mutate(color = cut(n, breaks=quantile(n)))

# Baaaaad factor practice. Do as I say, not as I do?!
levels(tornadocount$color) <- colors

Joining data

states@data <- left_join(states@data, tornadocount, by=c("NAME"="STATE"))
## Warning in left_join_impl(x, y, by$x, by$y, suffix$x, suffix$y): joining
## character vector and factor, coercing into character vector

Putting it all together (?)

# Not currently working
leaflet(data=states) %>%
  addTiles() %>%
  addPolygons(stroke = FALSE, fillOpacity = 0.5, smoothFactor = 0.5, color = ~states@data$color)

Modifiable areal unit problem

An introduction to the Modifiable Areal Unit Problem

Gerrymandering

The best explanation of gerrymandering you will ever see